//
//  DesPosterVC.m
//  HappyTrip
//
//  Created by yuxuesong on 15-3-30.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DesPosterVC.h"
#import "DesTripPhotoCollectionViewCell.h"
#import "DesSmallPhoto_albumObject.h"
#import "MJRefresh.h"

#define POSTERCELLIdentifier @"posterCellReuse"

@interface DesPosterVC ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)NSMutableArray *photosArr;
@property (nonatomic, retain)UICollectionView *collectionView;

@end

@implementation DesPosterVC

- (void)dealloc
{
    [_posterSendId release];
    [_posterSendType release];
    [_photosArr release];
    [_collectionView release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor lightGrayColor];
    
    self.photosArr = [NSMutableArray array];
    [self handlePhotosData];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(VIEW_WIDTH , VIEW_HEIGHT);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.minimumLineSpacing = 0;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT + 49) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    _collectionView.backgroundColor = [UIColor blackColor];
    [_collectionView registerClass:[DesTripPhotoCollectionViewCell class] forCellWithReuseIdentifier:POSTERCELLIdentifier];
    [self.view addSubview:_collectionView];
    [_collectionView release];
}

#pragma mark - UICollectionView 协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.photosArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DesTripPhotoCollectionViewCell *posterCell = [collectionView dequeueReusableCellWithReuseIdentifier:POSTERCELLIdentifier forIndexPath:indexPath];
    posterCell.desTripPhotoObject = [self.photosArr objectAtIndex:indexPath.item];
    return posterCell;
}

- (void)handlePhotosData
{
    NSString *urlStr = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/photos/?start=0&count=63&gallery_mode=true", self.posterSendType, self.posterSendId];
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [NSString stringWithFormat:@"%@/%ld.happyTripPlacePoster", docPath, [urlStr hash]];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        BOOL result = [NSKeyedArchiver archiveRootObject:responseObject toFile:path];
        NSLog(@"存储成功: %d", result);
        NSMutableArray *itemsArr = [NSMutableArray array];
        itemsArr = [responseObject objectForKey:@"items"];
        for (NSMutableDictionary *dic in itemsArr) {
            DesSmallPhoto_albumObject *smallPhtots = [[DesSmallPhoto_albumObject alloc] initWithDic:dic];
            [self.photosArr addObject:smallPhtots];
            [smallPhtots release];
        }
        _collectionView.contentOffset = CGPointMake(self.indexNumber * VIEW_WIDTH, 0);
        [self.collectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        id resultID = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        NSMutableArray *itemsArr = [NSMutableArray array];
        itemsArr = [resultID objectForKey:@"items"];
        for (NSMutableDictionary *dic in itemsArr) {
            DesSmallPhoto_albumObject *smallPhtots = [[DesSmallPhoto_albumObject alloc] initWithDic:dic];
            [self.photosArr addObject:smallPhtots];
            [smallPhtots release];
        }
        _collectionView.contentOffset = CGPointMake(self.indexNumber * VIEW_WIDTH, 0);
        [self.collectionView reloadData];
        static NSInteger i = 1;
        if (1 == i) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力,请检查网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
            [alertView release];
        }
    }];
}

- (void)leftBarButtonItemAction:(UIBarButtonItem *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
